package general

import (
	"github.com/zeromicro/go-zero/rest/httpx"
	"net/http"
	"yangming-college/app/general/cmd/v1/api/internal/logic/general"
	"yangming-college/app/general/cmd/v1/api/internal/svc"
)

func UploadHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		file, header, err := r.FormFile("image")
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
			return
		}

		l := general.NewUploadLogic(r.Context(), svcCtx)
		resp, err := l.Upload(file, header)
		if err != nil {
			httpx.ErrorCtx(r.Context(), w, err)
		} else {
			httpx.OkJsonCtx(r.Context(), w, resp)
		}
	}
}
